package com.huawei.updatesdk.service.appmgr.bean;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import com.huawei.fastengine.BuildConfig;
import com.huawei.hms.aaid.constant.AaidIdConstant;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.huawei.updatesdk.service.a.e;
import com.huawei.updatesdk.service.e.c;
import com.tencent.mm.opensdk.constants.ConstantsAPI.AppSupportContentFlag;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class a extends e {
    public static final String APIMETHOD = "client.https.diffUpgrade";
    public static final int DEFAULT_UPGRADE_RESULT = 0;
    public static final int FULL_UPGRADE_RESULT = 1;
    public static final int INSTALL_CHECK_DEFAULT = 0;
    private static final int PARSE_IS_REMOVABLE_PREINSTALLED_APK = 33554432;
    public static final int PRE_DOWNLOAD_CLOSE = 0;
    private static final String TAG = "UpgradeRequest";
    private static final int TYPE_NOT_PREINSTALL = 0;
    private static final int TYPE_PREINSTALL = 2;
    private static final int TYPE_PREINSTALL_REMOVABLE = 1;
    private String cmp_ = "1";
    private int installCheck_ = 0;
    private int isFullUpgrade_ = 0;
    private int isWlanIdle_ = 0;
    private a json_;
    private String maxMem_;

    public static class a extends com.huawei.updatesdk.a.b.d.a.b {
        private List<b> params_;

        public void a(List<b> list) {
            this.params_ = list;
        }
    }

    public static class b extends com.huawei.updatesdk.a.b.d.a.b {
        private String fSha2_;
        private int isPre_;
        private int maple_;
        private String oldVersion_;
        private String package_;
        private String sSha2_;
        private int targetSdkVersion_;
        private int versionCode_;

        public b(PackageInfo packageInfo) {
            this.package_ = packageInfo.packageName;
            this.versionCode_ = packageInfo.versionCode;
            this.oldVersion_ = packageInfo.versionName == null ? HwAccountConstants.NULL : packageInfo.versionName;
            this.targetSdkVersion_ = packageInfo.applicationInfo.targetSdkVersion;
            this.isPre_ = a.b(packageInfo);
            this.maple_ = c.b(this.package_);
            if (packageInfo.signatures != null) {
                this.sSha2_ = com.huawei.updatesdk.a.a.c.e.c(com.huawei.updatesdk.a.a.c.a.a(com.huawei.updatesdk.a.a.c.e.d(packageInfo.signatures[0].toCharsString())));
            }
            this.fSha2_ = com.huawei.updatesdk.a.a.c.c.a(packageInfo.applicationInfo.sourceDir, AaidIdConstant.SIGNATURE_SHA256);
        }
    }

    public static a a(List<PackageInfo> list) {
        a aVar = new a();
        aVar.b(APIMETHOD);
        aVar.e(String.valueOf(com.huawei.updatesdk.a.a.c.a.a.b(com.huawei.updatesdk.a.b.a.a.a().b()) / AppSupportContentFlag.MMAPP_SUPPORT_XLS));
        aVar.c("1.2");
        aVar.c(0);
        a aVar2 = new a();
        aVar.a(aVar2);
        List arrayList = new ArrayList();
        aVar2.a(arrayList);
        for (PackageInfo bVar : list) {
            arrayList.add(new b(bVar));
        }
        return aVar;
    }

    private static boolean a(ApplicationInfo applicationInfo) {
        int i = applicationInfo.flags;
        Integer a = c.a();
        if (a != null && (i & a.intValue()) != 0) {
            return true;
        }
        Field b = c.b();
        if (b != null) {
            try {
                if ((b.getInt(applicationInfo) & PARSE_IS_REMOVABLE_PREINSTALLED_APK) != 0) {
                    return true;
                }
            } catch (IllegalAccessException e) {
                com.huawei.updatesdk.a.a.b.a.a.a.a(TAG, "can not get hwflags" + e.toString());
            } catch (IllegalArgumentException e2) {
                com.huawei.updatesdk.a.a.b.a.a.a.a(TAG, "can not get hwflags" + e2.toString());
            }
        }
        return false;
    }

    private static int b(PackageInfo packageInfo) {
        return (packageInfo.applicationInfo.flags & 1) == 0 ? 0 : a(packageInfo.applicationInfo) ? 1 : 2;
    }

    public static a d(String str) {
        PackageInfo packageInfo = new PackageInfo();
        packageInfo.packageName = str;
        packageInfo.versionName = BuildConfig.VERSION_NAME;
        packageInfo.versionCode = 1;
        ApplicationInfo applicationInfo = new ApplicationInfo();
        applicationInfo.targetSdkVersion = 19;
        packageInfo.applicationInfo = applicationInfo;
        List arrayList = new ArrayList();
        arrayList.add(packageInfo);
        a a = a(arrayList);
        a.b(1);
        return a;
    }

    public void a(int i) {
        this.installCheck_ = i;
    }

    public void a(a aVar) {
        this.json_ = aVar;
    }

    public void b(int i) {
        this.isFullUpgrade_ = i;
    }

    public void c(int i) {
        this.isWlanIdle_ = i;
    }

    public void e(String str) {
        this.maxMem_ = str;
    }
}
